/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package guiexample;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import java.io.*;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;

public class TankView extends JFrame {

    Tank tank = null; //new Tank();
    TankController controller = null;
    //Image image = null;
    public TankView(Tank tank, TankController controller) {
        //initial values for tank
        this.tank = tank;
        this.controller = controller;
        this.addMouseListener(controller);
        this.addKeyListener(controller);
        this.addMouseMotionListener(controller);
        tank.setX(250);
        tank.setY(400);
        tank.setWidth(40);
        tank.setHeight(56);

   //     try
   // {
   //   image = ImageIO.read(new File("test2.jpg"));
   // }
   // catch (Exception e) { /*handled in paintComponent()*/ }
    }

    public void paint(Graphics g) {
        //move the tank across the screen
        Image image = null;
        Graphics2D bg = null;
        if(image != null){
            try{
                image = ImageIO.read(new File("test2.jpg"));
            } catch (IOException ex) {
                Logger.getLogger(TankView.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Recreated");
        }

        
        bg = (Graphics2D)image.getGraphics();
        //bg.setColor(Color.white);
        AffineTransform af;
        bg.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
        //bg.fillRect(0, 0, getWidth(), getHeight());
        bg.setColor(Color.black);
        int currentX = (int)tank.getX();
        int currentY = (int)tank.getY();
        int width = (int)tank.getWidth();
        int height = (int)tank.getHeight();
        Rectangle rect = new Rectangle(currentX, currentY, width, height);
        bg.rotate(Math.toRadians(3*Math.PI), rect.height / 2, rect.width / 2);
        bg.translate(currentX, currentY);
        bg.draw(rect);
        //bg.drawRect((int)tank.getX(), (int)tank.getY(),(int)tank.getWidth(), (int)tank.getHeight());
        int line1X = (int)tank.getX() + (int)(tank.getWidth()/2);
        int line1Y = (int)tank.getY() + (int)(tank.getHeight()/2);
        //vector triangle for turret
        bg.drawLine(line1X, line1Y, TankController.mouseX, TankController.mouseY);
        bg.drawLine(TankController.mouseX, TankController.mouseY, TankController.mouseX, line1Y);
        bg.drawLine(TankController.mouseX, line1Y, line1X, line1Y);
        g.drawImage(image, 0, 0, this);
    }
}
